package qeb.handheldmedical.patient;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import qeb.handheldmedical.R;
import qeb.handheldmedical.helper.DateTimePickDialogUtil;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.InPatient;
import qeb.handheldmedical.model.User;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2017年2月14日 上午11:05:52
 * @declaration
 */
public class EditTemperature extends Activity {
	InPatient ip = new InPatient();

	private Button btn_title_back;
	private Button btn_title_right;
	private TextView tv_title;
	private User usr = new User();
	private String initStartDateTime;// 初始化开始时间
	private qeb.handheldmedical.model.Temperature temperature;
	private EditText edtClsj, edtTw, edtmb, edthx, edtxy1, edtxy2, edtbc;
	private RadioButton radtwlx0, radtwlx1, radtwlx2;
	private RadioButton radjw0, radjw1, radjw2;
	private Button btnOk;
	private RadioGroup radjw, radtwlx;
	private String strTwlx;
	private String strCllx;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		ip = (InPatient) getIntent().getSerializableExtra("patientinfo");
		usr = (User) getIntent().getSerializableExtra("userinfo");
		try {
			temperature = (qeb.handheldmedical.model.Temperature) getIntent()
					.getSerializableExtra("temperature");
		} catch (Exception e) {
		}
		setContentView(R.layout.edit_temperature);
		initView();

	}

	private void initView() {

		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00");
		initStartDateTime = sDateFormat.format(new java.util.Date());

		tv_title = (TextView) findViewById(R.id.tv_top_title);
		tv_title.setText(ip.XM + "的体温记录");
		btn_title_back = (Button) findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(MyOnClickListener);
		btn_title_right = (Button) findViewById(R.id.btn_title_right);
		btn_title_right.setVisibility(View.VISIBLE);
		btnOk = (Button) findViewById(R.id.btnOk);
		btnOk.setOnClickListener(MyOnClickListener);
		edtClsj = (EditText) findViewById(R.id.edtClsj);
		edtTw = (EditText) findViewById(R.id.edtTw);
		edtmb = (EditText) findViewById(R.id.edtmb);
		edthx = (EditText) findViewById(R.id.edthx);
		edtxy1 = (EditText) findViewById(R.id.edtxy1);
		edtxy2 = (EditText) findViewById(R.id.edtxy2);
		edtbc = (EditText) findViewById(R.id.edtbc);
		radtwlx0 = (RadioButton) findViewById(R.id.radtwlx0);
		radtwlx1 = (RadioButton) findViewById(R.id.radtwlx1);
		radtwlx2 = (RadioButton) findViewById(R.id.radtwlx2);

		radjw0 = (RadioButton) findViewById(R.id.radjw0);
		radjw1 = (RadioButton) findViewById(R.id.radjw1);
		radjw2 = (RadioButton) findViewById(R.id.radjw2);

		edtClsj.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
						EditTemperature.this, initStartDateTime);
				dateTimePicKDialog.dateTimePicKDialog(edtClsj);

			}
		});

		// cllx 降温　0 半小时,1一小时
		// twjl 体温类型　0口温,1,腋温,2肛温
		if (temperature != null) {
			edtClsj.setText("20" + temperature.CLSJ);
			edtTw.setText(temperature.TW);
			edtmb.setText(temperature.MB);
			edthx.setText(temperature.HX);
			edtxy1.setText(temperature.XY1);
			edtxy2.setText(temperature.XY2);
			edtbc.setText(temperature.BC);

			if (temperature.TWLX.equals("0")) {
				radtwlx0.setChecked(true);
			} else if (temperature.TWLX.equals("1")) {
				radtwlx1.setChecked(true);
			} else if (temperature.TWLX.equals("2")) {
				radtwlx2.setChecked(true);
			} else {
				radtwlx0.setChecked(true);
			}

			if (temperature.CLLX.equals("0")) {
				radjw0.setChecked(true);
			} else if (temperature.CLLX.equals("1")) {
				radjw1.setChecked(true);
			} else if (temperature.CLLX.equals("2")) {
				radjw2.setChecked(true);
			} else {
				radjw0.setChecked(true);
			}

		} else {
			edtClsj.setText(initStartDateTime);
		}

	}

	private OnClickListener MyOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			// Bundle bundle=new Bundle();

			Intent intent = new Intent();

			switch (v.getId()) {
			case R.id.btn_title_back:// 返回按钮
				intent.putExtra("UPDATE", "0");
				break;
			case R.id.btnOk:// 保存更新体温
				String jsonString;
				String strAutoid = temperature == null ? ""
						: temperature.AUTOID;
				String strBc = temperature == null ? "" : temperature.BC;
				strCllx = temperature == null ? "" : temperature.CLLX;
				String strClsj = temperature == null ? "" : temperature.CLSJ;
				String strHx = temperature == null ? "" : temperature.HX;
				String strJlsj = temperature == null ? "" : temperature.JLSJ;
				String StrMb = temperature == null ? "" : temperature.MB;
				String strNid = temperature == null ? "" : temperature.NID;
				String strSg = temperature == null ? "" : temperature.SG;
				String strSjJlsj = temperature == null ? ""
						: temperature.SJJLSJ;
				String strTw = temperature == null ? "" : temperature.TW;
				String strTwJllb = temperature == null ? ""
						: temperature.TWJLLB;
				strTwlx = temperature == null ? "" : temperature.TWLX;
				String strTz = temperature == null ? "" : temperature.TZ;
				String strXy1 = temperature == null ? "" : temperature.XY1;
				String strXy2 = temperature == null ? "" : temperature.XY2;
				String strYbhd = temperature == null ? "" : temperature.YBHD;
				String strZyxh = ip.ZYXH;
				// {"AUTOID":"9b7f59e8-63b0-448d-9263-8bf5a27b991d","BC":"","CLLX":"0","CLSJ":"14-12-10 14:00","HX":"20",
				// "JLSJ":"2014-12-10 14:06:41","MB":"72","NID":"29ff62a5-1da7-45a0-a08d-dd2dc3dbf4e7","SG":"",
				// "SJJLSJ":"2014-12-10 12:20:21","TW":"36.5","TWJLLB":"","TWLX":"1","TZ":"","XY1":"133","XY2":"65",
				// "YBHD":"","ZYXH":"10047278"}
				strBc = edtbc.getText().toString().trim();
				strClsj = edtClsj.getText().toString().trim();
				strHx = edthx.getText().toString().trim();

				Date date = new Date();

				SimpleDateFormat sdformat24 = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				strJlsj = sdformat24.format(date);
				StrMb = edtmb.getText().toString().trim();
				strNid = usr.DID;
				strSjJlsj = strJlsj;
				strTw = edtTw.getText().toString().trim();
				strXy1 = edtxy1.getText().toString().trim();
				strXy2 = edtxy2.getText().toString().trim();
				if (radjw0.isChecked()) {
					strCllx = "0";
				} else if (radjw1.isChecked()) {
					strCllx = "1";
				} else if (radjw0.isChecked()) {
					strCllx = "2";
				} else {
					strCllx = "0";
				}

				if (radtwlx0.isChecked()) {
					strTwlx = "0";
				} else if (radtwlx1.isChecked()) {
					strTwlx = "1";
				} else if (radtwlx2.isChecked()) {
					strTwlx = "2";
				} else {
					strTwlx = "0";
				}

				if (temperature == null) {
					// 保存
					jsonString = String
							.format("{\"AUTOID\":\"%1$s\",\"BC\":\"%2$s\",\"CLLX\":\"%3$s\",\"CLSJ\":\"%4$s\",\"HX\":\"%5$s\",\"JLSJ\":\"%6$s\",\"MB\":\"%7$s\",\"NID\":\"%8$s\",\"SG\":\"%9$s\",\"SJJLSJ\":\"%10$s\",\"TW\":\"%11$s\",\"TWJLLB\":\"%12$s\",\"TWLX\":\"%13$s\",\"TZ\":\"%14$s\",\"XY1\":\"%15$s\",\"XY2\":\"%16$s\",\"YBHD\":\"%17$s\",\"ZYXH\":\"%18$s\"}",
									"", strBc, strCllx, strClsj, strHx,
									strJlsj, StrMb, strNid, strSg, strSjJlsj,
									strTw, strTwJllb, strTwlx, strTz, strXy1,
									strXy2, strYbhd, strZyxh);
					if (usr.BQID != null) {
						Map<String, String> map = new HashMap<String, String>();
						map.put("twjljson", jsonString);
						map.put("type", "1");

						JSONArray rejson = WSHelper.getWebserviceFun(
								"InsertOrUpdateTwjl", map);
						try {
							JSONObject reObject = rejson.getJSONObject(0);
							if (reObject.getString("pda_error_state").equals(
									"0")) {
								intent.putExtra("UPDATE", "1");
							} else {
								intent.putExtra("UPDATE", "0");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				} else {
					// 更新
					jsonString = String
							.format("{\"AUTOID\":\"%1$s\",\"BC\":\"%2$s\",\"CLLX\":\"%3$s\",\"CLSJ\":\"%4$s\",\"HX\":\"%5$s\",\"JLSJ\":\"%6$s\",\"MB\":\"%7$s\",\"NID\":\"%8$s\",\"SG\":\"%9$s\",\"SJJLSJ\":\"%10$s\",\"TW\":\"%11$s\",\"TWJLLB\":\"%12$s\",\"TWLX\":\"%13$s\",\"TZ\":\"%14$s\",\"XY1\":\"%15$s\",\"XY2\":\"%16$s\",\"YBHD\":\"%17$s\",\"ZYXH\":\"%18$s\"}",
									strAutoid, strBc, strCllx, strClsj, strHx,
									strJlsj, StrMb, strNid, strSg, strSjJlsj,
									strTw, strTwJllb, strTwlx, strTz, strXy1,
									strXy2, strYbhd, strZyxh);
					if (usr.BQID != null) {
						Map<String, String> map = new HashMap<String, String>();
						map.put("twjljson", jsonString);
						map.put("type", "2");

						JSONArray rejson = WSHelper.getWebserviceFun(
								"InsertOrUpdateTwjl", map);
						try {
							JSONObject reObject = rejson.getJSONObject(0);
							if (reObject.getString("pda_error_state").equals(
									"0")) {

								intent.putExtra("UPDATE", "1");

							} else {
								intent.putExtra("UPDATE", "0");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				break;
			}
			EditTemperature.this.setResult(RESULT_OK, intent);
			EditTemperature.this.finish();
		}
	};

}
